//
//  analysisManager.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/7/20.
//  Copyright © 2016年 hddden. All rights reserved.
//

import UIKit

//import AEXML

class analysisManager: NSObject,XMLParserDelegate {
    
    var elementName:String?
    var tempDict:[String:String]?
    var currentString:String?
//    var dictArray:[String:AnyObject?]?
    
    static let shareManager:analysisManager = {
        return analysisManager()
    }()
    static let shareManager2:analysisManager = {
        return analysisManager()
    }()
    override init() {
        super.init()
    }
    //MARK: - coreMethod
    func XmlParserAnalysis(_ parser:XMLParser, elementName:String) -> ([[String:String]]){
        //设置代理
        parser.delegate = self
        self.elementName = elementName
        self.dictArray.removeAll()
        parser.parse()
        return self.dictArray
    }
    
    
    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
        if elementName == self.elementName {
            //返回对应的数组
//            self.currentString = String()
            self.tempDict = [String:String]()
        }
        self.currentString = String()
    }
    
    func parser(_ parser: XMLParser, foundCharacters string: String) {
        self.currentString = self.currentString! + string
    }
    
    func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        if elementName != self.elementName{
            _ = self.tempDict?.updateValue(self.currentString!, forKey: elementName)
        }
        if elementName == self.elementName {
            self.dictArray.append(self.tempDict!)
            self.tempDict = nil
        }
    }
    
    lazy fileprivate var dictArray:[[String:String]] = {
        let temp = [[String:String]]()
        return temp
    }()
    
}
